<?php
$txtHoten=replatestring($_POST[txtHoten],false);
$txtNgaysinh=replatestring($_POST[txtNgaysinh],false);
$txtGioitinh=replatestring($_POST[txtGioitinh],false);
$txtHonnhan=replatestring($_POST[txtHonnhan],false);
$txtCMND=replatestring($_POST[txtCMND],false);
$txtNgaycap=replatestring($_POST[txtNgaycap],false);
$txtNoicap=replatestring($_POST[txtNoicap],false);
$txtSothich=replatestring($_POST[txtSothich],false);

$txtTencongty=replatestring($_POST[txtTencongty],false);
$txtViettat=replatestring($_POST[txtViettat],false);
$txtMaSoThue=replatestring($_POST[txtMaSoThue],false);
$txtDKKD=replatestring($_POST[txtDKKD],false);
$txtDaidien=replatestring($_POST[txtDaidien],false);
$txtChucvu=replatestring($_POST[txtChucvu],false);
$txtNamthanhlap=replatestring($_POST[txtNamthanhlap],false);
$TKnganhang=replatestring($_POST[txtTaikhoannganhang],false);

$txtDiachi=replatestring($_POST[txtDiachi],false);
$txtDienthoai=replatestring($_POST[txtDienthoai],false);
$txtDiDong=replatestring($_POST[txtDiDong],false);
$txtFax=replatestring($_POST[txtFax],false);

$txtEmail=replatestring($_POST[txtEmail],false);
$txtNickChat=replatestring($_POST[txtNickChat],false);
$txtWebsite=replatestring($_POST[txtWebsite],false);
$txtGhichu=replatestring($_POST[txtGhichu],false);
$IDNguongoc=replatestring($_POST[IDNguongoc],false);
$Lkh_Id=replatestring($_POST[Lkh_Id],false);
$Lvh_Id=replatestring($_POST[Lvh_Id],false);
$Cdm_Id=replatestring($_POST[Cdm_Id],false);
$market=$_POST['market'];
$idmkt=$_POST['marketing_id'];
if($idmkt==""){$idmkt=$market;}
$sanphamsudung=$_POST['sanphamsudung'];
$txtSanpham=$_POST['sanphamsudung_id'];
if($txtSanpham==""){$txtSanpham=$sanphamsudung;}
$donviquanly=$_POST['donviquanly'];
$txtNguoiquanly=$_POST['txtNguoiquanly'];



$sltinhthanh=$_POST["sltinhthanh"];
$quanhuyen=$_POST["idquanhuyen"];

$txtNguoitao=$_SESSION['userid'];
$txtNgaytao=time();
$txtnguoisua=$_SESSION['name'];
$txtNgaycapnhat=time();
$txtnoidung=$_POST['txtnoidung'];
$rdocanhan=$_POST['rdocanhan'];
$id=replatestring($_POST[id],false);
$soanh=$_POST['hdnsoanh'];
$hinhanhsp = "";
for($i=1;$i<=$soanh;$i++){
	if($i<$soanh)$hinhanhsp .= $_POST["sanpham_hinhanh".$i]."~";
	else $hinhanhsp .= $_POST["sanpham_hinhanh".$i];
}
$date=time();
$nguoiql=$_POST['nguoiql'];
$Nguoi_Quan_Ly_Khac=$_POST['Nguoi_Quan_Ly_Khac'];	
if($Nguoi_Quan_Ly_Khac==""){$Nguoi_Quan_Ly_Khac=$nguoiql;}
$data=false;
if($txtHoten!=="" or $txtTencongty!==""){$data=true;}

if($data==true){
	if($id==""){
		if($rdocanhan!=="" and $rdocanhan==0){
		mysql_query("insert into customers 
(name_kh,birthday,sex,status,card_id,ngaycap,noicap,sothich,address,telephone,mobile,fax,email,nick_name,website,notes,nguon_kh,loai_kh,area,product_use,marketing,manager_unit,manager,manager_other,creater,creat_time,user_edit,update_time,description,file_attach,object,tinhthanh_id,quan_huyen,care)
		value('$txtHoten','$txtNgaysinh','$txtGioitinh','$txtHonnhan','$txtCMND','$txtNgaycap','$txtNoicap','$txtSothich','$txtDiachi','$txtDienthoai','$txtDiDong','$txtFax','$txtEmail','$txtNickChat','$txtWebsite','$txtGhichu','$IDNguongoc','$Lkh_Id','$Lvh_Id','$txtSanpham','$idmkt','$donviquanly','$txtNguoiquanly','$Nguoi_Quan_Ly_Khac','$txtNguoitao','$date','$txtnguoisua','$date','$txtnoidung','$hinhanhsp','1','$sltinhthanh', '$quanhuyen','7')
		");
		
		}
		if($rdocanhan!=="" and $rdocanhan==1){
			mysql_query("insert into customers 
(name_com,name_brief,codetax,number_dkkd,present,position,nam_thanh_lap,tk_bank,address,telephone,mobile,fax,email,nick_name,website,notes,nguon_kh,loai_kh,area,product_use,marketing,manager_unit,manager,manager_other,creater,creat_time,update_time,description,file_attach,object,tinhthanh_id,quan_huyen,care)
		value('$txtTencongty','$txtViettat','$txtMaSoThue','$txtDKKD','$txtDaidien','$txtChucvu','$txtNamthanhlap','$TKnganhang','$txtDiachi','$txtDienthoai','$txtDiDong','$txtFax','$txtEmail','$txtNickChat','$txtWebsite','$txtGhichu','$IDNguongoc','$Lkh_Id','$Lvh_Id','$txtSanpham','$idmkt','$donviquanly','$txtNguoiquanly','$Nguoi_Quan_Ly_Khac','$txtNguoitao','$date','$txtnguoisua','$date','$txtnoidung','$hinhanhsp','2','7')
		");
		}
		$cauthongbao="Thêm thành công";
		$link=$url_main_site."system/index.php?tn=customers";
		thongbao($cauthongbao,$link);			

	}
	else
	{
		
			mysql_query("
					update customers set 
						name_kh='$txtHoten',
						name_com='$txtTencongty',
						name_brief='$txtViettat',
						codetax='$txtMaSoThue',
						number_dkkd='$txtDKKD',
						present='$txtDaidien',
						position='$txtChucvu',
						nam_thanh_lap='$txtNamthanhlap',
						tk_bank='$TKnganhang',
						birthday='$txtNgaysinh',
						sex='$txtGioitinh',
						status='$txtHonnhan',
						card_id='$txtCMND',
						ngaycap='$txtNgaycap',
						noicap='$txtNoicap',
						sothich='$txtSothich',
						address='$txtDiachi',
						telephone='$txtDienthoai',
						mobile='$txtDiDong',
						fax='$txtFax',
						email='$txtEmail',
						nick_name='$txtNickChat',
						website='$txtWebsite',
						notes='$txtGhichu',
						nguon_kh='$IDNguongoc',
						loai_kh='$Lkh_Id',
						area='$Lvh_Id',
						product_use='$txtSanpham',
						marketing='$idmkt',
						manager='$txtNguoiquanly',
						manager_unit='$donviquanly',
						manager='$txtNguoiquanly',
						manager_other='$Nguoi_Quan_Ly_Khac',
						user_edit='$txtnguoisua',
						update_time='$date',
						description='$txtnoidung'
					where id='$id'
			");
			if($hinhanhsp!="")
			{
			mysql_query("update customers set file_attach='$hinhanhsp' where id='$id'");
			}
			$cauthongbao="Bạn đã sửa thành công";
			$link=$url_main_site."system/index.php?tn=customers";
			thongbao($cauthongbao,$link);	

	}
}
else
{
		$cauthongbao="Dữ liệu không hợp lệ";
		$link=$url_main_site."system/index.php?tn=customers";
		thongbao($cauthongbao,$link);
}
?>